
Imports System.IO
Imports System

Public Class frmStructureGenerator

  ' Normal DataTypes

  Public iSubClassCounter As Integer = 0

  Public sSubClassArray(iSubClassCounter) As String
  Dim iSubClassMbrCount(iSubClassCounter) As Integer
  Dim iSubClassPack(iSubClassCounter) As Integer
  Dim sSubClassScope(iSubClassCounter) As String
  Dim iSubClassID(0) As String
  Dim blMainClassExists As Boolean

  Dim sSubClassName(0) As String
  Dim sClassMem(0) As String
  Dim sMemPosition(0) As String
  Dim sMemScope(0) As String
  Dim sMemDataType(0) As String
  Dim sMemClassNameType(0) As String
  Dim sMemArraySize(0) As String
  Dim sMemNullTerminated(0) As String
  Dim sMemConstantVal(0) As String
  Dim sMemTagValue(0) As String
  Dim sMemVrblType(0) As String
  Dim iMemSubClassID(0) As String


  Private Sub frmStructureGenerator_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    InitForm()
  End Sub

  Private Sub InitForm()

    txtSubClass.Enabled = False
    cboSubClsPack.Enabled = False
    cboSubClsScope.Enabled = False

    cboSubClsPack.Items.Add("1")
    cboSubClsPack.Items.Add("2")
    cboSubClsPack.Items.Add("3")
    cboSubClsPack.Items.Add("4")
    cboSubClsPack.SelectedIndex = 0

    cboSubClsScope.Items.Add(constPRIVATE)
    cboSubClsScope.Items.Add(constPUBLIC)
    cboSubClsScope.SelectedIndex = 0

    'For Normal Class Members

    cboMemScope.Items.Add(constPRIVATE)
    cboMemScope.Items.Add(constPUBLIC)
    cboMemScope.SelectedIndex = 0

    'For Structures , Character Arrays etc

    cboMemScope1.Items.Add(constPRIVATE)
    cboMemScope1.Items.Add(constPUBLIC)
    cboMemScope1.SelectedIndex = 0

    cboArrayDataType.Items.Add(const_vb_CHAR)
    cboArrayDataType.Items.Add(const_vb_DOUBLE)
    cboArrayDataType.Items.Add(const_vb_SINGLE)
    cboArrayDataType.Items.Add(const_vb_INT)
    cboArrayDataType.Items.Add(const_vb_SHORT)
    cboArrayDataType.Items.Add(const_vb_SHORT_INT16)
    cboArrayDataType.Items.Add(const_vb_UNSIGNED_CHAR)
    cboArrayDataType.Items.Add(const_vb_UNSIGNEDINT)
    cboArrayDataType.Items.Add(const_vb_UNSIGNEDSHORT)
    cboArrayDataType.SelectedIndex = 0

    'For Global variables of a class
    cboGlbMemScope.Items.Add(constPRIVATE)
    cboGlbMemScope.Items.Add(constPUBLIC)
    cboGlbMemScope.SelectedIndex = 0

    rdoFixed.Checked = True

    cboNullTermntd.Items.Add(constNullTerminatedNO)
    cboNullTermntd.Items.Add(constNullTerminatedYES)
    cboNullTermntd.SelectedIndex = 0

    PopulateMemberType()

    Call DisableClassControls()

    blMainClassExists = False

    'open DataBase
    mPrRefreshConn()

  End Sub

  Private Sub ResetControls()
    txtMainClass.Clear()
    txtSubClass.Clear()
    rdoFixed.Checked = True
    cboSubClsPack.SelectedIndex = 0
    cboSubClsScope.SelectedIndex = 0
    txtGlbMem.Clear()
    cboGlbMemScope.SelectedIndex = 0
    txtGlbMemValue.Clear()
    txtMem.Clear()
    cboMemScope.SelectedIndex = 0
    cboMemType.SelectedIndex = 0
    txtMemConstVal.Clear()
    txtMemTagValue.Clear()
    txtMem1.Clear()
    cboMemScope1.SelectedIndex = 0
    cboMemType1.SelectedIndex = 0
    txtMemSize1.Clear()
    txtMemClassName1.Clear()
    txtMemTagValue1.Clear()
    cboNullTermntd.SelectedIndex = 0
    cboArrayDataType.SelectedIndex = 0
    DataGridView.Rows.Clear()
  End Sub

  Private Sub DisableClassControls()

    txtSubClass.Enabled = False
    cboSubClsPack.Enabled = False
    cboSubClsScope.Enabled = False

    Global_Var.Enabled = False
    Normal_Members.Enabled = False
    Others.Enabled = False

    cmdGenerate.Enabled = False
    cmdClear.Enabled = False
    cmdDelRow.Enabled = False

  End Sub

  Private Sub EnableClassControls()

    txtSubClass.Enabled = True
    cboSubClsPack.Enabled = True
    cboSubClsScope.Enabled = True

    Global_Var.Enabled = True
    Normal_Members.Enabled = True
    Others.Enabled = True

    cboMemScope.SelectedIndex = 0
    cboMemType.SelectedIndex = 0
    cboMemScope1.SelectedIndex = 0
    cboMemType1.SelectedIndex = 0

    txtSubClass.Text = String.Empty
    txtMem.Text = String.Empty
    txtMemSize1.Text = String.Empty
    txtMem1.Text = String.Empty

  End Sub

  Private Sub PopulateMemberType()

    If rdoFixed.Checked = True Then

      cboMemType.Items.Clear()
      cboMemType.Items.Add(const_vb_CHAR)
      cboMemType.Items.Add(const_vb_STRING)
      cboMemType.Items.Add(const_vb_DOUBLE)
      cboMemType.Items.Add(const_vb_SINGLE)
      cboMemType.Items.Add(const_vb_INT)
      cboMemType.Items.Add(const_vb_SHORT)
      cboMemType.Items.Add(const_vb_SHORT_INT16)
      cboMemType.Items.Add(const_vb_UNSIGNED_CHAR)
      cboMemType.Items.Add(const_vb_UNSIGNEDINT)
      cboMemType.Items.Add(const_vb_UNSIGNEDSHORT)
      cboMemType.SelectedIndex = 0

    ElseIf rdoFixed.Checked = False Then

      cboMemType.Items.Clear()
      cboMemType.Items.Add(const_vb_CHAR)
      cboMemType.Items.Add(const_vb_DOUBLE)
      cboMemType.Items.Add(const_vb_SINGLE)
      cboMemType.Items.Add(const_vb_INT)
      cboMemType.Items.Add(const_vb_SHORT)
      cboMemType.Items.Add(const_vb_SHORT_INT16)
      cboMemType.Items.Add(const_vb_UNSIGNED_CHAR)
      cboMemType.Items.Add(const_vb_UNSIGNEDINT)
      cboMemType.Items.Add(const_vb_UNSIGNEDSHORT)
      cboMemType.SelectedIndex = 0

    End If

    If rdoOthers.Checked = True Then

      cboMemType1.Items.Clear()
      cboMemType1.Items.Add(constARRAY)
      cboMemType1.Items.Add(constUSERDEFINED)
      cboMemType1.Items.Add(constUSERDEFINEDARRAY)
      cboMemType1.SelectedIndex = 0

    Else

      cboMemType1.Items.Clear()
      cboMemType1.Items.Add(constUSERDEFINED)
      cboMemType1.Items.Add(constUSERDEFINEDARRAY)
      cboMemType1.SelectedIndex = 0

    End If

  End Sub


  Private Sub cmdViewClasses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewClasses.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.Main_Classes
    FrmDisplayGrid.sCallingControl = "txtMainClass"
    FrmDisplayGrid.ShowDialog()
  End Sub

  Private Sub cmdViewSubClasses_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdViewSubClasses.Click

    If Len(txtMainClass.Text) < 1 Then
      MsgBox("Please specify the class name before adding members for that class", MsgBoxStyle.Exclamation, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If mPrRefreshConn() = False Then Exit Sub

  End Sub

  Private Sub cmdAddSubClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddSubClass.Click
    Dim objMcls As New clsMainclass
    Dim slct_rslt As Integer

    If Len(txtMainClass.Text) < 1 Then
      MsgBox("Please specify the class name before adding members for that class", MsgBoxStyle.Exclamation, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If mPrRefreshConn() = False Then Exit Sub

    objMcls.mStrMainClassName = Trim(txtMainClass.Text)
    slct_rslt = objMcls.SelectMclsBasdName()

    If slct_rslt = 1 Then

      Dim msgresult As MsgBoxResult
      msgresult = MsgBox("The entries for the mentioned Main Class already exists..Do you want to append to existing file?", MsgBoxStyle.YesNo, gCONST_MSG_HEADER)
      If msgresult = MsgBoxResult.No Then
        If DBDisconnect() = False Then Exit Sub
        Exit Sub
      Else
        blMainClassExists = True
      End If

    Else
      blMainClassExists = False
    End If

    If DBDisconnect() = False Then Exit Sub

    Call EnableClassControls()

    Call cboMemType1_SelectedIndexChanged(sender, e)

  End Sub


  Private Sub cmdAddMem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddMem.Click
    Dim iCounter As Integer = 0

    If Len(txtSubClass.Text) < 1 Then
      MsgBox("The SubClass name cannot be blank", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If Len(txtMem.Text) < 1 Then
      MsgBox("The Member name cannot be blank", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If


    If rdoFixed.Checked = True Then

      If Trim(txtMemTagValue.Text) = String.Empty Then
        MsgBox("Please Select the Tag value", MsgBoxStyle.Information, gCONST_MSG_HEADER)
        Exit Sub
      End If

    End If

    With DataGridView
      .Rows.Add(1)
    End With

    iCounter = (DataGridView.RowCount - 1)

    With DataGridView

      .Rows(iCounter).Cells(1).Value = txtSubClass.Text
      .Rows(iCounter).Cells(2).Value = txtMem.Text
      .Rows(iCounter).Cells(3).Value = fnGetPostionCounter()
      .Rows(iCounter).Cells(4).Value = cboMemScope.Text
      .Rows(iCounter).Cells(5).Value = cboMemType.Text
      .Rows(iCounter).Cells(6).Style.BackColor = Color.LightGray
      .Rows(iCounter).Cells(6).ReadOnly = True
      .Rows(iCounter).Cells(7).Style.BackColor = Color.LightGray
      .Rows(iCounter).Cells(7).ReadOnly = True
      .Rows(iCounter).Cells(8).Style.BackColor = Color.LightGray
      .Rows(iCounter).Cells(8).ReadOnly = True
      .Rows(iCounter).Cells(9).Value = txtMemConstVal.Text
      .Rows(iCounter).Cells(10).Value = txtMemTagValue.Text
      .Rows(iCounter).Cells(11).Value = "Normal"

    End With

    txtMem.Text = String.Empty
    cboMemScope.SelectedIndex = 0
    cboMemType.SelectedIndex = 0
    txtMemTagValue.Text = String.Empty
    txtMemConstVal.Text = String.Empty
    cmdGenerate.Enabled = True
    cmdClear.Enabled = True
    cmdDelRow.Enabled = True

  End Sub

  Private Sub cmdAddMem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddMem2.Click
    Dim iCounter As Integer = 0

    If Len(txtSubClass.Text) < 1 Then
      MsgBox("The SubClass name cannot be blank", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If Len(txtMem1.Text) < 1 Then
      MsgBox("The Member name cannot be blank", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If


    If rdoFixed.Checked = True Then
      If (cboMemType1.Text = constARRAY) And Trim(txtMemTagValue1.Text) = String.Empty Then
        Dim msgreslt As MsgBoxResult
        msgreslt = MsgBox("Tag Value has not been selected....Do you want to continue?", MsgBoxStyle.YesNo, gCONST_MSG_HEADER)

        If msgreslt = MsgBoxResult.No Then
          Exit Sub
        End If

      End If
    End If

    If (cboMemType1.Text = constARRAY Or cboMemType1.Text = constUSERDEFINEDARRAY) And Trim(txtMemSize1.Text) = String.Empty Then
      MsgBox("Please Enter the size for the array", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If (cboMemType1.Text = constUSERDEFINED) And Trim(txtMemClassName1.Text) = String.Empty Then
      MsgBox("Please Enter the class name of which the member is a type", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    If (cboMemType1.Text = constUSERDEFINEDARRAY) And Trim(txtMemClassName1.Text) = String.Empty Then
      MsgBox("Please Enter the class name of which the member is a type", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      Exit Sub
    End If

    With DataGridView
      .Rows.Add(1)
    End With

    iCounter = (DataGridView.RowCount - 1)

    Select Case (cboMemType1.Text)

      Case constARRAY

        With DataGridView
          .Rows(iCounter).Cells(1).Value = txtSubClass.Text
          .Rows(iCounter).Cells(2).Value = txtMem1.Text
          .Rows(iCounter).Cells(3).Value = fnGetPostionCounter()
          .Rows(iCounter).Cells(4).Value = cboMemScope1.Text
          .Rows(iCounter).Cells(5).Value = cboArrayDataType.Text
          .Rows(iCounter).Cells(6).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(6).ReadOnly = True
          .Rows(iCounter).Cells(7).Value = txtMemSize1.Text
          .Rows(iCounter).Cells(8).Value = cboNullTermntd.Text
          .Rows(iCounter).Cells(9).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(9).ReadOnly = True
          .Rows(iCounter).Cells(10).Value = txtMemTagValue1.Text
          .Rows(iCounter).Cells(11).Value = cboMemType1.Text
        End With

      Case constUSERDEFINEDARRAY

        With DataGridView
          .Rows(iCounter).Cells(1).Value = txtSubClass.Text
          .Rows(iCounter).Cells(2).Value = txtMem1.Text
          .Rows(iCounter).Cells(3).Value = fnGetPostionCounter()
          .Rows(iCounter).Cells(4).Value = constPUBLIC 'cboMemScope1.Text
          .Rows(iCounter).Cells(5).Value = cboMemType1.Text
          .Rows(iCounter).Cells(6).Value = txtMemClassName1.Text
          .Rows(iCounter).Cells(7).Value = txtMemSize1.Text
          .Rows(iCounter).Cells(8).Value = cboNullTermntd.Text
          .Rows(iCounter).Cells(9).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(9).ReadOnly = True
          .Rows(iCounter).Cells(10).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(10).ReadOnly = True
          .Rows(iCounter).Cells(11).Value = cboMemType1.Text

        End With

      Case constUSERDEFINED

        With DataGridView
          .Rows(iCounter).Cells(1).Value = txtSubClass.Text
          .Rows(iCounter).Cells(2).Value = txtMem1.Text
          .Rows(iCounter).Cells(3).Value = fnGetPostionCounter()
          .Rows(iCounter).Cells(4).Value = constPUBLIC 'cboMemScope1.Text
          .Rows(iCounter).Cells(5).Value = cboMemType1.Text
          .Rows(iCounter).Cells(6).Value = txtMemClassName1.Text
          .Rows(iCounter).Cells(7).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(7).ReadOnly = True
          .Rows(iCounter).Cells(8).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(8).ReadOnly = True
          .Rows(iCounter).Cells(9).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(9).ReadOnly = True
          .Rows(iCounter).Cells(10).Style.BackColor = Color.LightGray
          .Rows(iCounter).Cells(10).ReadOnly = True
          .Rows(iCounter).Cells(11).Value = cboMemType1.Text
        End With

    End Select

    txtMem1.Text = String.Empty
    cboMemScope1.SelectedIndex = 0
    cboMemType1.SelectedIndex = 0
    cboArrayDataType.SelectedIndex = 0
    cboNullTermntd.SelectedIndex = 0
    txtMemSize1.Text = String.Empty
    txtMemClassName1.Text = String.Empty
    txtMemTagValue1.Text = String.Empty
    cmdGenerate.Enabled = True
    cmdClear.Enabled = True
    cmdDelRow.Enabled = True

  End Sub

  Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click

    Dim mbrValue As MsgBoxResult
    Dim iCntr As Integer = 0
    Dim iArrayCount As Integer = 0
    Dim iMainClassID As Integer
    Dim iCounter As Integer = DataGridView.RowCount

    ReDim Preserve sSubClassName(iCounter)
    ReDim Preserve iMemSubClassID(iCounter)
    ReDim Preserve sClassMem(iCounter)
    ReDim Preserve sMemPosition(iCounter)
    ReDim Preserve sMemScope(iCounter)
    ReDim Preserve sMemDataType(iCounter)
    ReDim Preserve sMemClassNameType(iCounter)
    ReDim Preserve sMemArraySize(iCounter)
    ReDim Preserve sMemNullTerminated(iCounter)
    ReDim Preserve sMemConstantVal(iCounter)
    ReDim Preserve sMemTagValue(iCounter)
    ReDim Preserve sMemVrblType(iCounter)

    Dim objMcls As New clsMainclass
    Dim objScls As New clsScls
    Dim objVrbl As New clsVrbl
    ReDim Preserve iSubClassID(iCounter)

    mbrValue = MsgBox("Are Sure you want to proceed with generation of the structure's class and the corresponding code ?", MsgBoxStyle.YesNo)

    If mbrValue = MsgBoxResult.Yes Then
      mPrRefreshConn()
      Me.Cursor = Cursors.AppStarting

      'Making an entry in MCLS

      objMcls.mStrMainClassName = txtMainClass.Text
      objMcls.mStrMainClassScope = constPUBLIC

      If rdoFixed.Checked = True Then
        objMcls.mStrMainClassCatg = constClsCatgFIXX
      Else
        objMcls.mStrMainClassCatg = constClsCatgOTHR
      End If

      If blMainClassExists = False Then

        iMainClassID = objMcls.fnInsertMcls

      Else

        objMcls.fnSelectMclsIdxxBasdName()

        While objMcls.mDrMainClass.Read
          iMainClassID = objMcls.mDrMainClass(0).ToString()
        End While

      End If


      'Making and entry in SCLS

      For iArrayCount = 0 To (iSubClassCounter - 1)

        objScls.mStrSubClassName = sSubClassArray(iArrayCount)
        objScls.mIntSubClassPack = iSubClassPack(iArrayCount)
        objScls.mStrSubClassScope = sSubClassScope(iArrayCount)
        objScls.mIntSubClassId = iMainClassID

        iSubClassID(iArrayCount) = objScls.fnInsertScls()

      Next

      For iCntr = 0 To (DataGridView.RowCount - 1)

        With DataGridView

          sSubClassName(iCntr) = .Rows(iCntr).Cells(1).Value
          iMemSubClassID(iCntr) = fnGetSubClassID(.Rows(iCntr).Cells(1).Value)
          sClassMem(iCntr) = .Rows(iCntr).Cells(2).Value
          sMemPosition(iCntr) = .Rows(iCntr).Cells(3).Value
          sMemScope(iCntr) = .Rows(iCntr).Cells(4).Value
          sMemDataType(iCntr) = .Rows(iCntr).Cells(5).Value
          sMemClassNameType(iCntr) = .Rows(iCntr).Cells(6).Value
          sMemArraySize(iCntr) = .Rows(iCntr).Cells(7).Value
          sMemNullTerminated(iCntr) = .Rows(iCntr).Cells(8).Value
          sMemConstantVal(iCntr) = .Rows(iCntr).Cells(9).Value
          sMemTagValue(iCntr) = .Rows(iCntr).Cells(10).Value
          sMemVrblType(iCntr) = .Rows(iCntr).Cells(11).Value

        End With

      Next

      'Call the class to insert value into the Tables 
      For iCntr = 0 To (DataGridView.RowCount - 1)

        objVrbl.mIntVrblSqnce = sMemPosition(iCntr)
        objVrbl.mStrVrblName = sClassMem(iCntr)
        objVrbl.mIntVrblMclsIdxx = iMainClassID
        objVrbl.mIntVrblSclsIdxx = iMemSubClassID(iCntr)
        objVrbl.mStrVrblDataType = sMemDataType(iCntr)
        objVrbl.mStrVrblScope = sMemScope(iCntr)
        objVrbl.mStrVrblType = sMemVrblType(iCntr)
        objVrbl.mStrVrblNullIndc = sMemNullTerminated(iCntr)
        objVrbl.mStrVrblSize = sMemArraySize(iCntr)
        objVrbl.mStrVrblRefrName = sMemClassNameType(iCntr)
        objVrbl.mStrVrblFixxName = sMemTagValue(iCntr)

        objVrbl.fnInsertVrbl()

      Next

      objMcls = Nothing
      objScls = Nothing
      objVrbl = Nothing

      'Call code to Generate the class file for the structure by passing the main class id as input
      Dim objGnrtStruct As New clsGnrtStruct

      objGnrtStruct.mIntMainClassId = iMainClassID
      objGnrtStruct.GenerateStructure()

      MsgBox("The Class File generated as C:\cls" + txtMainClass.Text + ".vb", MsgBoxStyle.Information, gCONST_MSG_HEADER)

      'Call code to generate the Code for the class created by passing the main class id as input

      If rdoFixed.Checked = True Then
        gfnCodeGenerator(iMainClassID)
        MsgBox("The Class Code generated as C:\cls" + txtMainClass.Text + "Packet.vb", MsgBoxStyle.Information, gCONST_MSG_HEADER)
      End If

      If con.State = ConnectionState.Open Then
        DBDisconnect()
      End If

      Me.Cursor = Cursors.Default

    Else


      Exit Sub

    End If

  End Sub

  Private Sub cboMemType1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboMemType1.SelectedIndexChanged
    Select Case (cboMemType1.Text)

      Case constARRAY

        lblMemSize1.Visible = True
        txtMemSize1.Visible = True
        cmdArraySizeDisplay.Visible = True
        lblNullTermntd.Visible = True
        cboNullTermntd.Visible = True
        lblArrayDataType.Visible = True
        cboArrayDataType.Visible = True
        lblTagValue1.Visible = True
        txtMemTagValue1.Visible = True
        cmdTag1.Visible = True

        'Class Name is not applicable

        lblMemClass.Visible = False
        txtMemClassName1.Visible = False
        cmdSubClassdisplay.Visible = False

        ' Array Size , Array Data Type and tag Value is applicable

        cmdArraySizeDisplay.Enabled = True
        lblArrayDataType.Enabled = True
        cboArrayDataType.Enabled = True
        cboArrayDataType.Enabled = True
        lblNullTermntd.Enabled = True
        cboNullTermntd.Enabled = True
        cboNullTermntd.Enabled = True
        txtMemSize1.Enabled = True
        cmdArraySizeDisplay.Enabled = True
        txtMemTagValue1.Enabled = True
        cmdTag1.Enabled = True

      Case constUSERDEFINEDARRAY

        lblMemSize1.Visible = True
        txtMemSize1.Visible = True
        cmdArraySizeDisplay.Visible = True
        lblNullTermntd.Visible = True
        cboNullTermntd.Visible = True
        lblArrayDataType.Visible = False
        cboArrayDataType.Visible = False
        lblTagValue1.Visible = False
        txtMemTagValue1.Visible = False
        cmdTag1.Visible = False

        'Class Name is not applicable

        lblMemClass.Visible = True
        txtMemClassName1.Visible = True
        cmdSubClassdisplay.Visible = True

        'Array Size, Class name is applicable

        txtMemSize1.Enabled = True
        txtMemClassName1.Visible = True
        txtMemClassName1.Enabled = True
        cmdArraySizeDisplay.Enabled = True
        lblNullTermntd.Enabled = True
        cboNullTermntd.Enabled = True
        cmdSubClassdisplay.Enabled = True

        'Array datatype and Tag value are not applicable

        lblArrayDataType.Enabled = False
        cboArrayDataType.Enabled = False
        txtMemTagValue1.Enabled = False
        cmdTag1.Enabled = False


      Case constUSERDEFINED

        lblMemSize1.Visible = False
        txtMemSize1.Visible = False
        cmdArraySizeDisplay.Visible = False
        lblNullTermntd.Visible = False
        cboNullTermntd.Visible = False
        lblArrayDataType.Visible = False
        cboArrayDataType.Visible = False
        lblTagValue1.Visible = False
        txtMemTagValue1.Visible = False
        cmdTag1.Visible = False

        'Class Name is not applicable

        lblMemClass.Visible = True
        txtMemClassName1.Visible = True
        cmdSubClassdisplay.Visible = True

        ' Class Name is applicable

        txtMemClassName1.Visible = True
        txtMemClassName1.Enabled = True
        cmdSubClassdisplay.Enabled = True

        'Array size, array datatype and Tag value are not applicable

        txtMemSize1.Enabled = False
        cmdArraySizeDisplay.Enabled = False
        lblArrayDataType.Enabled = False
        cboArrayDataType.Enabled = False
        lblNullTermntd.Enabled = False
        cboNullTermntd.Enabled = False
        txtMemTagValue1.Enabled = False
        cmdTag1.Enabled = False

    End Select
  End Sub

  Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
    Dim userchoice As MsgBoxResult

    userchoice = MsgBox("Are you sure you want to delete all the details entered for the current main class ?", MsgBoxStyle.YesNo)

    If userchoice = MsgBoxResult.No Then
      Exit Sub
    Else
      userchoice = MsgBoxResult.Yes
      txtMainClass.Text = String.Empty
      txtSubClass.Text = String.Empty
      txtMem.Text = String.Empty
      txtMemTagValue.Text = String.Empty
      txtMem1.Text = String.Empty
      txtMemTagValue1.Text = String.Empty
      txtMemSize1.Text = String.Empty
      txtMemClassName1.Text = String.Empty

      cmdGenerate.Enabled = False
      cmdClear.Enabled = False
      cmdDelRow.Enabled = False

      With DataGridView
        .Rows.Clear()
      End With

      Call DisableClassControls()

      iSubClassCounter = 0
      blMainClassExists = False

      ReDim sSubClassArray(0)
      ReDim iSubClassMbrCount(0)
      ReDim iSubClassPack(0)
      ReDim sSubClassScope(0)
      ReDim iSubClassID(0)

      ReDim iMemSubClassID(0)
      ReDim sSubClassName(0)
      ReDim sClassMem(0)
      ReDim sMemPosition(0)
      ReDim sMemScope(0)
      ReDim sMemDataType(0)
      ReDim sMemClassNameType(0)
      ReDim sMemArraySize(0)
      ReDim sMemNullTerminated(0)
      ReDim sMemConstantVal(0)
      ReDim sMemTagValue(0)
      ReDim sMemVrblType(0)

      If con.State = ConnectionState.Open Then
        DBDisconnect()
      End If
    End If
  End Sub



  'Private Sub cmdAddGlbMem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddGlbMem.Click

  '    Dim iCounter As Integer = 0
  '    Dim newrow As New DataGridViewRow

  '    If Trim(txtGlbMemValue.Text) = string.empty Then
  '        MsgBox("Enter the value for the constant", MsgBoxStyle.Information, gmessage_header)
  '        Exit Sub
  '    End If

  '    With DataGridView
  '        .Rows.Add(newrow)
  '    End With

  '    iCounter = DataGridView.RowCount - 1

  '    With DataGridView
  '        .Rows(iCounter).Cells(1).Value = "Global Variable"
  '        .Rows(iCounter).Cells(2).Value = txtGlbMem.Text
  '        .Rows(iCounter).Cells(3).Value = string.empty
  '        .Rows(iCounter).Cells(4).Value = cboGlbMemScope.Text
  '        .Rows(iCounter).Cells(5).Value = "Integer"
  '        .Rows(iCounter).Cells(6).Value = string.empty
  '        .Rows(iCounter).Cells(7).Value = string.empty
  '        .Rows(iCounter).Cells(8).Value = txtGlbMemValue.Text

  '        .Rows(iCounter).Cells(3).ReadOnly = True
  '        .Rows(iCounter).Cells(6).ReadOnly = True
  '        .Rows(iCounter).Cells(7).ReadOnly = True
  '    End With

  '    cmdGenerate.Enabled = True
  '    cmdClear.Enabled = True
  '    cmdDelRow.Enabled = True
  '    txtGlbMem.Text = string.empty
  '    txtGlbMemValue.Text = string.empty

  'End Sub

  'Private Sub txtMainClass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMainClass.TextChanged

  '  lblGlbMem.Enabled = True
  '  lblGlbMemScope.Enabled = True
  '  lblGlbMemValue.Enabled = True
  '  txtGlbMem.Enabled = True
  '  cboGlbMemScope.Enabled = True
  '  txtGlbMemValue.Enabled = True
  '  cmdAddGlbMem.Enabled = True
  '  txtGlbMem.Text = string.empty
  '  txtGlbMemValue.Text = string.empty

  'End Sub


  Private Sub cmdDelRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelRow.Click

    Dim iRowPostn As Integer = 0
    Dim row As New DataGridViewRow
    Dim sSubClassName As String = String.Empty
    Dim iPosition As Integer = 0
    Dim iRowCount As Integer = 0

    While iRowPostn < (DataGridView.RowCount)

      With DataGridView

        If .Rows(iRowPostn).Cells(0).Value = True Then

          row = DataGridView.Rows(iRowPostn)

          'If a particular row is deleted then the position counter for other members of
          'that subclass needs to be re-ordered

          If Len(.Rows(iRowPostn).Cells(3).Value) > 0 Then

            sSubClassName = .Rows(iRowPostn).Cells(1).Value
            iPosition = .Rows(iRowPostn).Cells(3).Value

            With DataGridView

              For iRowCount = 0 To (.RowCount - 1)

                If .Rows(iRowCount).Cells(1).Value = sSubClassName And _
                   .Rows(iRowCount).Cells(3).Value > CStr(iPosition) Then

                  .Rows(iRowCount).Cells(3).Value = (.Rows(iRowCount).Cells(3).Value) - 1

                End If

              Next

            End With

          End If

          DataGridView.Rows.Remove(row)
          subAdjustArrayPositionCounter(sSubClassName)

        Else
          iRowPostn = iRowPostn + 1
        End If

      End With

    End While


  End Sub

  Public Function fnGetPostionCounter() As Integer
    Dim iArrayIndex As Integer = 0

    'For a member being added to a sub class

    For iArrayIndex = 0 To (iSubClassCounter - 1)
      If sSubClassArray(iArrayIndex) = txtSubClass.Text Then
        iSubClassMbrCount(iArrayIndex) = iSubClassMbrCount(iArrayIndex) + 1
        fnGetPostionCounter = iSubClassMbrCount(iArrayIndex)
        Exit Function
      End If
    Next

    'For first member being added add entries for the sub class parameters

    ReDim Preserve sSubClassArray(iSubClassCounter)
    ReDim Preserve iSubClassMbrCount(iSubClassCounter)
    ReDim Preserve iSubClassPack(iSubClassCounter)
    ReDim Preserve sSubClassScope(iSubClassCounter)

    sSubClassArray(iSubClassCounter) = txtSubClass.Text
    iSubClassMbrCount(iSubClassCounter) = 1
    iSubClassPack(iSubClassCounter) = cboSubClsPack.Text
    sSubClassScope(iSubClassCounter) = cboSubClsScope.Text


    fnGetPostionCounter = iSubClassMbrCount(iSubClassCounter)
    iSubClassCounter = iSubClassCounter + 1

  End Function
  Public Function fnGetSubClassID(ByVal sSubClassName As String) As Integer
    Dim iArrayIndex As Integer = 0
    fnGetSubClassID = 0

    For iArrayIndex = 0 To (iSubClassCounter - 1)
      If sSubClassArray(iArrayIndex) = sSubClassName Then
        fnGetSubClassID = iSubClassID(iArrayIndex)
        Exit Function
      End If
    Next

  End Function
  Public Sub subAdjustArrayPositionCounter(ByVal sSubClassName As String)
    Dim iIndex As Integer = 0
    Dim iCount As Integer = 0

    For iIndex = 0 To (iSubClassCounter - 1)
      If sSubClassArray(iIndex) = sSubClassName Then

        iSubClassMbrCount(iIndex) = iSubClassMbrCount(iIndex) - 1

        'If because of deletion of rows the complete sub class entries are not needed
        'Move the remaining sub class details one position forward

        If iSubClassMbrCount(iIndex) = 0 Then

          iSubClassCounter = iSubClassCounter - 1

          If iSubClassCounter > 1 Then

            For iCount = iIndex To (iSubClassCounter - 1)

              sSubClassArray(iCount) = sSubClassArray(iCount + 1)
              iSubClassMbrCount(iCount) = iSubClassMbrCount(iCount + 1)
              iSubClassPack(iCount) = iSubClassPack(iCount + 1)
              sSubClassScope(iCount) = sSubClassScope(iCount + 1)

            Next
          End If

          ReDim Preserve sSubClassArray(iSubClassCounter)
          ReDim Preserve iSubClassMbrCount(iSubClassCounter)
          ReDim Preserve iSubClassPack(iSubClassCounter)
          ReDim Preserve sSubClassScope(iSubClassCounter)

          sSubClassArray(iSubClassCounter) = String.Empty
          iSubClassMbrCount(iSubClassCounter) = 0
          iSubClassPack(iSubClassCounter) = 0
          sSubClassScope(iSubClassCounter) = String.Empty

        End If
      End If
    Next

  End Sub

  Public Sub subClearSubClassArray()
    ReDim sSubClassArray(0)
    ReDim iSubClassMbrCount(0)
    ReDim iSubClassPack(0)
    ReDim sSubClassScope(0)

    sSubClassArray(0) = String.Empty
    iSubClassMbrCount(0) = 0
    iSubClassCounter = 0
    iSubClassPack(0) = 0
    sSubClassScope(0) = String.Empty
  End Sub

  Private Sub cboSubClsPack_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSubClsPack.SelectedIndexChanged
    Dim iCount As Integer = 0

    For iCount = 0 To (iSubClassCounter - 1)

      If sSubClassArray(iCount) = txtSubClass.Text Then

        iSubClassPack(iCount) = CInt(cboSubClsPack.Text)

      End If

    Next
  End Sub

  Private Sub cboSubClsScope_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboSubClsScope.SelectedIndexChanged
    Dim iCount As Integer = 0

    For iCount = 0 To (iSubClassCounter - 1)

      If sSubClassArray(iCount) = txtSubClass.Text Then

        sSubClassScope(iCount) = cboSubClsScope.Text

      End If

    Next
  End Sub
 
  Private Sub cmdTag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTag.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.Tag_Values
    FrmDisplayGrid.sCallingControl = "txtMemTagValue"
    FrmDisplayGrid.ShowDialog()
  End Sub

  Private Sub cmdTag1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTag1.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.Tag_Values
    FrmDisplayGrid.sCallingControl = "txtMemTagValue1"
    FrmDisplayGrid.ShowDialog()
  End Sub

     
    
  Private Sub cmdArraySizeDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdArraySizeDisplay.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.Constant_Values
    FrmDisplayGrid.sCallingControl = "txtMemSize1"
    FrmDisplayGrid.ShowDialog()
  End Sub

  Private Sub cmdSubClassdisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSubClassdisplay.Click
    FrmDisplayGrid.sCallingPurpose = gENMU_PUEPOSE.Sub_Class_Names
    FrmDisplayGrid.sCallingControl = "txtMemClassName1"
    FrmDisplayGrid.sEnteredMainName = Trim(txtMainClass.Text)
    FrmDisplayGrid.ShowDialog()
  End Sub

  Private Sub rdoFixed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoFixed.CheckedChanged
    PopulateMemberType()
  End Sub

  Private Sub rdoOthers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoOthers.CheckedChanged
    PopulateMemberType()
  End Sub

  Private Sub cmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReset.Click
    ResetControls()
    InitForm()
  End Sub

End Class
